/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.usuario.vista;

import basedatos.CrearBaseDeDatos;
import basedatos.OperacionesBD;
import java.awt.Cursor;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import modulos.respaldosBD.BackupBD;
import modulos.usuario.modelo.AlgoritmoAES;
import modulos.usuario.modelo.Enlace;
import modulos.usuario.modelo.Medicina;
import modulos.usuario.modelo.SerialMAC_AES;
import vista.ClsInicio;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author Edulima
 */
public class VtnSolicitarLicencia extends javax.swing.JDialog {

    public static final String FIRMA = "K1E9RC2L8PO3L7NA4G6M";

    /**
     * Creates new form VtnSolicitarLicencia
     */
    public VtnSolicitarLicencia(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel14 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnPrueba = new javax.swing.JButton();
        btnLicencia = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        txtSerial4 = new javax.swing.JTextField();
        txtSerial3 = new javax.swing.JTextField();
        txtSerial2 = new javax.swing.JTextField();
        txtSerial1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        lblWeb = new javax.swing.JLabel();

        jLabel14.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Medikux info.png"))); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 3, 13)); // NOI18N
        jLabel1.setText("Necesita una licencia para este programa");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(85, 131, 307, 17);

        btnPrueba.setText("Seguir en prueba");
        btnPrueba.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPruebaActionPerformed(evt);
            }
        });
        getContentPane().add(btnPrueba);
        btnPrueba.setBounds(100, 230, 140, 27);

        btnLicencia.setText("Usar licencia");
        btnLicencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLicenciaActionPerformed(evt);
            }
        });
        getContentPane().add(btnLicencia);
        btnLicencia.setBounds(250, 230, 140, 27);

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(177, 29, 29));
        jLabel2.setText("Nota:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(55, 280, 40, 17);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.gray));

        txtSerial4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSerial4KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSerial4KeyTyped(evt);
            }
        });

        txtSerial3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSerial3KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSerial3KeyTyped(evt);
            }
        });

        txtSerial2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSerial2KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSerial2KeyTyped(evt);
            }
        });

        txtSerial1.setColumns(5);
        txtSerial1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSerial1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSerial1KeyTyped(evt);
            }
        });

        jLabel4.setText("-");

        jLabel16.setText("-");

        jLabel17.setText("-");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(txtSerial1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSerial2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel16)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSerial3, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(jLabel17)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSerial4, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSerial1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSerial2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSerial3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSerial4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(47, 166, 392, 55);

        jLabel3.setText("Por favor ingrese la licencia del producto , caso");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(107, 280, 300, 17);

        jLabel7.setText("contrario haga clic en Seguir en prueba solo  ");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(107, 303, 300, 17);

        jLabel8.setText("podra ingresar 5 veces al sistema , debera ");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(107, 326, 290, 17);
        getContentPane().add(jLabel9);
        jLabel9.setBounds(407, 465, 0, 0);
        getContentPane().add(jLabel10);
        jLabel10.setBounds(407, 459, 0, 0);

        jLabel11.setText("contactar con algun representante, para");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(107, 349, 290, 17);

        jLabel12.setFont(new java.awt.Font("DejaVu Sans", 1, 11)); // NOI18N
        jLabel12.setText("Visite nuestra Pagina Web ");
        getContentPane().add(jLabel12);
        jLabel12.setBounds(107, 407, 174, 14);

        jLabel13.setText("solicitar su licencia ");
        getContentPane().add(jLabel13);
        jLabel13.setBounds(107, 372, 150, 17);

        jLabel15.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Medikux info.png"))); // NOI18N
        getContentPane().add(jLabel15);
        jLabel15.setBounds(12, 0, 490, 130);

        lblWeb.setFont(new java.awt.Font("DejaVu Sans", 1, 11)); // NOI18N
        lblWeb.setText("<html><a href=\"http://www.medikux.com/\">www.medikux.com</a></html>");
        lblWeb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblWebMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblWebMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblWebMouseExited(evt);
            }
        });
        getContentPane().add(lblWeb);
        lblWeb.setBounds(287, 407, 120, 14);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-504)/2, (screenSize.height-467)/2, 504, 467);
    }// </editor-fold>//GEN-END:initComponents

    private void btnPruebaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPruebaActionPerformed
        dispose();
        ClsInicio.ponePerfil();
        ClsInicio.logueandoUsuario();
    }//GEN-LAST:event_btnPruebaActionPerformed

    private void btnLicenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLicenciaActionPerformed
        try {
            dispose();
            String firma = txtSerial1.getText() + txtSerial2.getText() + txtSerial3.getText() + txtSerial4.getText();
            if (firma.equals(SerialMAC_AES.generarSerial())) {
                //ArrayList<Medicina> aux = (ArrayList<Medicina>) OperacionesBD.listar("Medicina");
//                ArrayList<Medicina> aux = CrearBaseDeDatos.listar();
//                Medicina medicina = aux.get(0);
//                AlgoritmoAES alg = new AlgoritmoAES(VtnLicencia.SERIAL.getBytes());
//                medicina.setMac(alg.encriptar(Enlace.getMac()));
                AlgoritmoAES alg = new AlgoritmoAES(VtnLicencia.SERIAL.getBytes());
                ClsInicio.medicina.setSerial(alg.encriptar(firma));
                CrearBaseDeDatos.modificar(ClsInicio.medicina);
                //OperacionesBD.modificar(medicina);
                String direc = getClass().getResource("/basedatos/CodigoCie10.sql").getPath();
                BackupBD.restaurarBackup(direc, ClsInicio.prop.getProperty("user"), ClsInicio.prop.getProperty("clave"));
                ClsInicio.ponePerfil();
                ClsInicio.logueandoUsuario();
            } else {
                JOptionPane.showMessageDialog(this, "Serial incorrecto", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(VtnSolicitarLicencia.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnLicenciaActionPerformed

    private void txtSerial1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial1KeyTyped
        OperacionesVarias.ingresoKeygen(evt, txtSerial1.getText());
    }//GEN-LAST:event_txtSerial1KeyTyped

    private void txtSerial1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial1KeyReleased
        txtSerial1.setText(txtSerial1.getText().toUpperCase());
        if (txtSerial1.getText().length() == 5) {
            txtSerial2.requestFocus();
        }
    }//GEN-LAST:event_txtSerial1KeyReleased

    private void txtSerial2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial2KeyReleased
        txtSerial2.setText(txtSerial2.getText().toUpperCase());
        if (txtSerial2.getText().length() == 5) {
            txtSerial3.requestFocus();
        }
    }//GEN-LAST:event_txtSerial2KeyReleased

    private void txtSerial3KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial3KeyReleased
        txtSerial3.setText(txtSerial3.getText().toUpperCase());
        if (txtSerial3.getText().length() == 5) {
            txtSerial4.requestFocus();
        }
    }//GEN-LAST:event_txtSerial3KeyReleased

    private void txtSerial4KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial4KeyReleased
        txtSerial4.setText(txtSerial4.getText().toUpperCase());
        if (txtSerial4.getText().length() == 5) {
            btnLicencia.requestFocus();
        }
    }//GEN-LAST:event_txtSerial4KeyReleased

    private void txtSerial2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial2KeyTyped
        OperacionesVarias.ingresoKeygen(evt, txtSerial2.getText());
    }//GEN-LAST:event_txtSerial2KeyTyped

    private void txtSerial3KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial3KeyTyped
        OperacionesVarias.ingresoKeygen(evt, txtSerial3.getText());
    }//GEN-LAST:event_txtSerial3KeyTyped

    private void txtSerial4KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSerial4KeyTyped
        OperacionesVarias.ingresoKeygen(evt, txtSerial4.getText());
    }//GEN-LAST:event_txtSerial4KeyTyped

    private void lblWebMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblWebMouseClicked
        OperacionesVarias.abrirURL("http://www.medikux.com");
    }//GEN-LAST:event_lblWebMouseClicked

    private void lblWebMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblWebMouseEntered
        lblWeb.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }//GEN-LAST:event_lblWebMouseEntered

    private void lblWebMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblWebMouseExited
        lblWeb.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_lblWebMouseExited

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VtnSolicitarLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VtnSolicitarLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VtnSolicitarLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VtnSolicitarLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VtnSolicitarLicencia dialog = new VtnSolicitarLicencia(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLicencia;
    private javax.swing.JButton btnPrueba;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblWeb;
    private javax.swing.JTextField txtSerial1;
    private javax.swing.JTextField txtSerial2;
    private javax.swing.JTextField txtSerial3;
    private javax.swing.JTextField txtSerial4;
    // End of variables declaration//GEN-END:variables
}
